﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;

namespace Delphi.Mimir.Data.Entities.Final.Mappings {
    public class QuoteMap : EntityTypeConfiguration<Quote> {
        public QuoteMap() {
            /******************************************************************
            /* Table
            /*****************************************************************/
            ToTable("fin.eod_quotes");

            /******************************************************************
            /* Primary Key
            /*****************************************************************/
            HasKey(a => a.QuoteId);

            /******************************************************************
            /* Index
            /*****************************************************************/
            // IX_QOT
            Property(a => a.SymbolId)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_QOT", 1) { IsUnique = true }));
            Property(a => a.TradeDate)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_QOT", 2) { IsUnique = true }));
            Property(a => a.FrequencyId)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_QOT", 3) { IsUnique = true }));

            /******************************************************************
            /* Relationships
            /*****************************************************************/
            // 1..n Symbol
            HasRequired<Symbol>(a => a.Symbol)
                .WithMany()
                .HasForeignKey(a => a.SymbolId);

            // 1..n Frequency
            HasRequired<Frequency>(a => a.Frequency)
                .WithMany()
                .HasForeignKey(a => a.FrequencyId);

            /******************************************************************
            /* Properties
            /*****************************************************************/
            Property(a => a.QuoteId)
                .IsRequired()
                .HasColumnName("qot_id");

            Property(a => a.SymbolId)
                .IsRequired()
                .HasColumnName("qot_sym_id");

            Property(a => a.TradeDate)
                .IsRequired()
                .HasColumnName("qot_trade_dt");

            Property(a => a.FrequencyId)
                .IsRequired()
                .HasColumnName("qot_cat_id");

            Property(a => a.Open)
                .IsRequired()
                .HasPrecision(18, 6)
                .HasColumnName("qot_open");

            Property(a => a.High)
                .IsRequired()
                .HasPrecision(18, 6)
                .HasColumnName("qot_high");

            Property(a => a.Low)
                .IsRequired()
                .HasPrecision(18, 6)
                .HasColumnName("qot_low");

            Property(a => a.Close)
                .IsRequired()
                .HasPrecision(18, 6)
                .HasColumnName("qot_close");

            Property(a => a.Volume)
                .IsOptional()
                .HasColumnName("qot_volume");

            Property(a => a.OpenInterest)
                .IsOptional()
                .HasColumnName("qot_open_interest");

            Property(a => a.AdjustedFactor)
                .IsOptional()
                .HasPrecision(18, 6)
                .HasColumnName("qot_adj_factor");
        }
    }
}
